package com.jinyang.jetpackdemo.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.bean.ArticleItem4
import com.jinyang.jetpackdemo.databinding.ActivityArticleList2Binding
import com.jinyang.jetpackdemo.databinding.LayoutItemArticleBinding
import com.jinyang.jetpackdemo.util.LjyLogUtil


class ArticleList2Activity : AppCompatActivity() {
    lateinit var mAdapter: ArticleAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LjyLogUtil.d("onCreate")
        val binding: ActivityArticleList2Binding =
            DataBindingUtil.setContentView(this, R.layout.activity_article_list2)
        binding.lifecycleOwner = this
        binding.rvArticleList.layoutManager = LinearLayoutManager(this)
        val articleList: MutableList<ArticleItem4> = ArrayList()
        articleList.add(ArticleItem4("title1", "jinYang", "content111"))
        articleList.add(ArticleItem4("title2", "jinYang", "content222"))
        articleList.add(ArticleItem4("title3", "jinYang", "content333"))
        mAdapter = ArticleAdapter(articleList)
        binding.rvArticleList.adapter = mAdapter
        binding.onClickPresenter2 = OnClickPresenter2()
    }

    inner class OnClickPresenter2 {
        fun changeItem() {
            mAdapter.data[0].title.value += "abc"
            mAdapter.data[0].author.value += "abc"
            mAdapter.data[0].content.value += "abc"
            mAdapter.notifyItemChanged(0)
        }

        fun addArticle() {
            mAdapter.addData(
                ArticleItem4(
                    "title${mAdapter.data.size}",
                    "jinYang",
                    "content${mAdapter.data.size}"
                )
            )
            LjyLogUtil.d("addArticle")
        }

        fun removeArticle() {
            mAdapter.removeAt(0)
            LjyLogUtil.d("removeArticle")
        }
    }
}


class ArticleAdapter(data: MutableList<ArticleItem4>?) :
    BaseQuickAdapter<ArticleItem4, BaseDataBindingHolder<LayoutItemArticleBinding>>(
        R.layout.layout_item_article,
        data
    ) {

    override fun convert(
        holder: BaseDataBindingHolder<LayoutItemArticleBinding>,
        item: ArticleItem4
    ) {
        holder.dataBinding?.articleInfo = item
        holder.dataBinding?.executePendingBindings()
    }
}